home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / carbon qt graphic import / source / windowcode.c < prev   
Encoding:
C/C++ Source or Header  |  2000-09-28  |  10.4 KB  |  362 lines

  1. /*
  2.     File:        WindowCode.c
  3.     
  4.     Description:Code for dealing with windows and the memory associated with each window.
  5.  
  6.     Author:        MC
  7.  
  8.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                 ("Apple") in consideration of your agreement to the following terms, and your
  12.                 use, installation, modification or redistribution of this Apple software
  13.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                 please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                 In consideration of your agreement to abide by the following terms, and subject
  17.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                 copyrights in this original Apple software (the "Apple Software"), to use,
  19.                 reproduce, modify and redistribute the Apple Software, with or without
  20.                 modifications, in source and/or binary forms; provided that if you redistribute
  21.                 the Apple Software in its entirety and without modifications, you must retain
  22.                 this notice and the following text and disclaimers in all such redistributions of
  23.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                 Apple Software without specific prior written permission from Apple.  Except as
  26.                 expressly stated in this notice, no other rights or licenses, express or implied,
  27.                 are granted by Apple herein, including but not limited to any patent rights that
  28.                 may be infringed by your derivative works or by other works in which the Apple
  29.                 Software may be incorporated.
  30.  
  31.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                 COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.                 
  45.     Change History (most recent first):
  46.  
  47. */
  48.  
  49. #define TARGET_API_MAC_CARBON 1
  50.  
  51. #include <LowMem.h>
  52. #include <TextUtils.h>
  53. #include <TextServices.h>
  54.  
  55. #include "WindowCode.h"
  56. #include "MovieCode.h"
  57. #include "GraphicImportDrawCode.h"
  58.  
  59. extern    long        gSleepTime;
  60. extern    Boolean        gInForeground;
  61.  
  62. void    DisplayAbout (void) {
  63.     Alert (500, nil);
  64. }
  65.  
  66. OSErr DispatchWindowUpdate (WindowPtr theWindow) {
  67.     OSErr                        err;
  68.     WindowInfoHandle            winInfo;
  69.  
  70.     err = noErr;
  71.  
  72.     if (theWindow != nil) {
  73.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  74.     }
  75.  
  76.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  77.         BeginUpdate (theWindow);
  78.         if ((**winInfo).winType == kGraphPicWinType) {
  79.             err = UpdateGraphWindow (theWindow, nil);
  80.         } else if ((**winInfo).winType == kMovieWinType) {
  81.             err = UpdateMovieWindow (theWindow);
  82.         }
  83.         EndUpdate (theWindow);
  84.     }
  85.  
  86.     return err;
  87. }
  88.  
  89. void DoActivate (EventRecord *event, Boolean kindOfActivate) {
  90.     WindowPtr                    theWindow;
  91.     WindowInfoHandle            winInfo;
  92.     short                        activateWindow,
  93.                                 activateProgram;
  94.     Boolean                        moviePlaying;
  95.     OSErr                        err;
  96.     SInt8                        hState;                        
  97.  
  98.     if (kindOfActivate == true) {
  99.         // It is a Activate/Deactivate event
  100.         activateWindow = (short)(event->modifiers & activeFlag);
  101.  
  102.         theWindow = (WindowPtr)event->message;
  103.  
  104.         if (theWindow != nil) {
  105.             winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  106.         }
  107.  
  108.         if (winInfo != nil && (**winInfo).sig == kMySig) {
  109.             hState = HGetState ((Handle)winInfo);
  110.             HLock ((Handle)winInfo);
  111.  
  112.             if (activateWindow) {
  113.                 // Make this window the active window
  114.                 if ((**winInfo).winType == kMovieWinType) {
  115.                     if ((**winInfo).winMovieController == nil) {
  116.                         // Movie has been disposed, get it back
  117.                         err = DrawMovie (&((**winInfo).winSpec), theWindow, (**winInfo).winMovieTime);
  118.                     }
  119.  
  120.                     MCActivate ((**winInfo).winMovieController, theWindow, true);
  121.                 }
  122.             } else {
  123.                 // Make this window no longer the active window
  124.                 // To save memory we close the component when the window goes into the background.
  125.                 if ((**winInfo).winMovieController != nil) {
  126.                     MCActivate ((**winInfo).winMovieController, theWindow, false);
  127.                     DisposeMovieController ((**winInfo).winMovieController);
  128.                     (**winInfo).winMovieController = nil;
  129.                 }
  130.                 if ((**winInfo).winMovie != nil) {
  131.                     DisposeMovie ((**winInfo).winMovie);
  132.                     (**winInfo).winMovie = nil;
  133.                 }
  134.                 if ((**winInfo).winGraphicImporter != nil) {
  135.                     CloseComponent ((**winInfo).winGraphicImporter);
  136.                     (**winInfo).winGraphicImporter = nil;
  137.                 }
  138.             }
  139.  
  140.             HSetState ((Handle)winInfo, hState);
  141.         }
  142.     } else {
  143.         // It is a Suspend/Resume event
  144.         activateProgram = (short)(event->message & suspendResumeMessage);
  145.  
  146.         // Turn off any menu hilite to correct a bug with AMO
  147.         HiliteMenu (0);
  148.  
  149.         theWindow = FrontWindow ();
  150.  
  151.         if (activateProgram) {
  152.             gInForeground = true;
  153.             gSleepTime = kForegroundIdleSleepTime;
  154.  
  155.             while (theWindow != nil) {
  156.                 winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  157.                 if (winInfo != nil && (**winInfo).sig == kMySig) {
  158.                     hState = HGetState ((Handle)winInfo);
  159.                     HLock ((Handle)winInfo);
  160.                     if ((**winInfo).winMovieController != nil) {
  161.                         if (theWindow == FrontWindow ()) {
  162.                             MCActivate ((**winInfo).winMovieController, theWindow, true);
  163.                         }
  164.                     }
  165.                     HSetState ((Handle)winInfo, hState);
  166.                 }
  167.                 theWindow = GetNextWindow (theWindow);
  168.             }
  169.         } else {
  170.             gInForeground = false;
  171.             while (theWindow != nil) {
  172.                 winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  173.                 if (winInfo != nil && (**winInfo).sig == kMySig) {
  174.                     hState = HGetState ((Handle)winInfo);
  175.                     HLock ((Handle)winInfo);
  176.                     if ((**winInfo).winMovieController != nil) {
  177.                         if (GetMovieRate ((**winInfo).winMovie)) {
  178.                             moviePlaying = true;
  179.                         }
  180.                         MCActivate ((**winInfo).winMovieController, theWindow, false);
  181.                     }
  182.                     HSetState ((Handle)winInfo, hState);
  183.                 }
  184.                 theWindow = GetNextWindow (theWindow);
  185.             }
  186.  
  187.             if (!moviePlaying) {
  188.                 gSleepTime = kBackgroundIdleSleepTime;
  189.             } else {
  190.                 gSleepTime = kBackgroundBusySleepTime;    // Keep the movies playing well
  191.             }
  192.         }
  193.     }
  194.  
  195.     return;
  196. }
  197.  
  198. void    ReleaseMemory (WindowInfoHandle winInfo, Boolean fullDispose) {
  199.     SInt8                        hState;                        
  200.  
  201.     if (winInfo != nil && (**winInfo).sig == kMySig && ((**winInfo).winType == kGraphPicWinType || (**winInfo).winType == kMovieWinType)) {
  202.         hState = HGetState ((Handle)winInfo);
  203.         HLock ((Handle)winInfo);
  204.  
  205.         if ((**winInfo).winGraphicImporter != nil) {
  206.             (void)CloseComponent ((**winInfo).winGraphicImporter);
  207.             (**winInfo).winGraphicImporter = nil;
  208.         }
  209.  
  210.         if ((**winInfo).winMovieController != nil) {
  211.             DisposeMovieController ((**winInfo).winMovieController);
  212.             (**winInfo).winMovieController = nil;
  213.         }
  214.  
  215.         if ((**winInfo).winMovie != nil) {
  216.             DisposeMovie ((**winInfo).winMovie);
  217.             (**winInfo).winMovie = nil;
  218.         }
  219.  
  220.         if ((**winInfo).winGWorld != nil) {
  221.             DisposeGWorld ((**winInfo).winGWorld);
  222.             (**winInfo).winGWorld = nil;
  223.         }
  224.  
  225.         if ((**winInfo).winDataHandle != nil) {
  226.             DisposeHandle ((**winInfo).winDataHandle);
  227.             (**winInfo).winDataHandle = nil;
  228.         }
  229.  
  230.         if ((**winInfo).flatPageFormatHandle != nil) {
  231.             DisposeHandle ((**winInfo).flatPageFormatHandle);
  232.             (**winInfo).flatPageFormatHandle = nil;
  233.         }
  234.  
  235.         if ((**winInfo).flatPrintSettingsHandle != nil) {
  236.             DisposeHandle ((**winInfo).flatPrintSettingsHandle);
  237.             (**winInfo).flatPrintSettingsHandle = nil;
  238.         }
  239.  
  240.         if (fullDispose == true) {
  241.             DisposeHandle ((Handle)winInfo);
  242.             winInfo = nil;
  243.         }
  244.  
  245.         HSetState ((Handle)winInfo, hState);
  246.     }
  247. }
  248.  
  249. void    CloseThisWindow (WindowPtr theWindow) {
  250.     RgnHandle            grayRgn;
  251.     WindowInfoHandle    winInfo;
  252.     Boolean                hasPalette,
  253.                         redrawMenuBar;
  254.  
  255.     if (theWindow != nil) {
  256.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  257.  
  258.         if (winInfo != nil && (**winInfo).sig == kMySig) {
  259.             hasPalette = false;
  260.         }
  261.  
  262.         ReleaseMemory (winInfo, true);
  263.         SetPalette (theWindow, nil, false);
  264.         DisposeWindow (theWindow);
  265.     }
  266.  
  267.     if (FrontWindow () == nil) {
  268.         MenuHandle    theFileMenu;
  269.  
  270.         ShowCursor ();
  271.  
  272.         theFileMenu = GetMenuHandle (mFile);
  273.         if (theFileMenu != nil) {
  274.             DisableMenuItem (theFileMenu, iClose);
  275.             DisableMenuItem (theFileMenu, iPageSetup);
  276.             DisableMenuItem (theFileMenu, iPrint);
  277.         }
  278.  
  279.         if (hasPalette == true) {
  280.             // Cause all background windows to redraw with the default palette
  281.             grayRgn = GetGrayRgn ();
  282.             PaintBehind (nil, grayRgn);
  283.             redrawMenuBar = true;
  284.         }
  285.     }
  286.  
  287.     if (redrawMenuBar == true) {
  288.         // Redraw the menu bar
  289.         DrawMenuBar ();
  290.     }
  291. }
  292.  
  293. OSErr    MakeNewWindow (Str255 name, Boolean visible, WindowPtr behindWindow, WindowPtr *theWindow) {
  294.     OSErr                        err;
  295.     WindowInfoHandle            winInfo;
  296.     WindowPtr                    newWindow;
  297.     Str255                        title;
  298.  
  299.     GetIndString (title, 130, 1);
  300.  
  301.     if (name != nil) {
  302.         BlockMoveData (name, title, name[0]+1);
  303.     }
  304.  
  305.     winInfo = (WindowInfoHandle)NewHandleClear (sizeof (WindowInfoRec));
  306.     if (winInfo != nil) {
  307.         Rect    boundsRect = {0, 7, 250, 210};
  308.  
  309.         (**winInfo).sig = kMySig;
  310.         (**winInfo).winType = kGraphPicWinType;
  311.         (**winInfo).winScale = 0.0;
  312.  
  313.         boundsRect.top = (short)(GetMBarHeight () + 22);
  314.         newWindow = NewCWindow (nil, &boundsRect, title, visible, documentProc, behindWindow, true, (long)winInfo);
  315.     }
  316.  
  317.     if (newWindow != nil) {
  318.         MenuHandle    theFileMenu;
  319.  
  320.         theFileMenu = GetMenuHandle (mFile);
  321.         if (theFileMenu != nil) {
  322.             EnableMenuItem (theFileMenu, iClose);
  323.             EnableMenuItem (theFileMenu, iPageSetup);
  324.             EnableMenuItem (theFileMenu, iPrint);
  325.         }
  326.  
  327.         *theWindow = newWindow;
  328.         err = noErr;
  329.     } else {
  330.         err = memFullErr;
  331.     }
  332.  
  333.     return err;
  334. }
  335.  
  336. static OSErr DeleteFile (FSSpec * file) {
  337.     return FSpDelete (file);
  338. }
  339.  
  340. OSErr MoveFrontWindowFileToTrash (void) {
  341.     OSErr                        err;
  342.     WindowPtr                    theWindow;
  343.     WindowInfoHandle            winInfo;
  344.     FSSpec                        fileToDelete;
  345.  
  346.     theWindow = FrontWindow ();
  347.  
  348.     if (theWindow != nil) {
  349.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  350.     }
  351.  
  352.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  353.         BlockMoveData (&(**winInfo).winSpec, &fileToDelete, sizeof (FSSpec));
  354.         CloseThisWindow (theWindow);
  355.         err = DeleteFile (&fileToDelete);
  356.     } else {
  357.         err = paramErr;
  358.     }
  359.  
  360.     return err;
  361. }
  362.